package org.dromara.tearoom.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.tearoom.domain.TerCleanerLog;
import org.dromara.tearoom.domain.TerRoomDetails;
import org.dromara.tearoom.domain.bo.TerCleanerLogBo;
import org.dromara.tearoom.domain.enums.RoomStatusEnum;
import org.dromara.tearoom.domain.vo.TerCleanerLogVo;
import org.dromara.tearoom.mapper.TerCleanerLogMapper;
import org.dromara.tearoom.mapper.TerRoomDetailsMapper;
import org.dromara.tearoom.service.ITerCleanerLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 保洁记录Service业务层处理
 *
 * @author ckj
 * @date 2025-04-18
 */
@RequiredArgsConstructor
@Service
public class TerCleanerLogServiceImpl implements ITerCleanerLogService {

    private final TerCleanerLogMapper baseMapper;

    private final TerRoomDetailsMapper roomDetailsMapper;

    /**
     * 查询保洁记录
     *
     * @param id 主键
     * @return 保洁记录
     */
    @Override
    public TerCleanerLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询保洁记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 保洁记录分页列表
     */
    @Override
    public TableDataInfo<TerCleanerLogVo> queryPageList(TerCleanerLogBo bo, PageQuery pageQuery) {
        Page<TerCleanerLogVo> result = baseMapper.selectPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的保洁记录列表
     *
     * @param bo 查询条件
     * @return 保洁记录列表
     */
    @Override
    public List<TerCleanerLogVo> queryList(TerCleanerLogBo bo) {
        LambdaQueryWrapper<TerCleanerLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TerCleanerLog> buildQueryWrapper(TerCleanerLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TerCleanerLog> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(TerCleanerLog::getCreateTime);
        lqw.eq(bo.getOrderId() != null, TerCleanerLog::getOrderId, bo.getOrderId());
        lqw.eq(bo.getRoomId() != null, TerCleanerLog::getRoomId, bo.getRoomId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), TerCleanerLog::getContent, bo.getContent());
        lqw.eq(bo.getSysUserId() != null, TerCleanerLog::getSysUserId, bo.getSysUserId());
        return lqw;
    }

    /**
     * 新增保洁记录
     *
     * @param bo 保洁记录
     * @return 是否新增成功
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(TerCleanerLogBo bo) {
        bo.setCreateBy(LoginHelper.getAppLoginUser().getId());
        TerRoomDetails roomDetails=roomDetailsMapper.selectById(bo.getRoomId());
        if(ObjectUtil.isEmpty(roomDetails)){
            throw new ServiceException("房间不存在");
        }
        TerCleanerLog add = MapstructUtils.convert(bo, TerCleanerLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        roomDetails.setStatus(RoomStatusEnum.NO_USED.getType());
        roomDetailsMapper.updateById(roomDetails);
        return flag;
    }

    /**
     * 修改保洁记录
     *
     * @param bo 保洁记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TerCleanerLogBo bo) {
        TerCleanerLog update = MapstructUtils.convert(bo, TerCleanerLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TerCleanerLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除保洁记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
